package com.wolfsea.binderdemo
import android.os.Binder
import android.os.IBinder
import android.os.Parcel

/**
 *@desc CalculateStub
 *@author liuliheng
 *@time 2022/10/23  17:39
 **/
class CalculateNumberStub : Binder(), ICalculateNumber {

    init {
        /** Construct the stub at attach it to the interface. */
        this.attachInterface(this, DESCRIPTOR)
    }

    override fun doSum(a: Int, b: Int): Int = a + b

    override fun doMinus(a: Int, b: Int): Int = a - b

    override fun getPid(): Int = android.os.Process.myPid()

    override fun asBinder(): IBinder = this

    override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean {
        val descriptor = DESCRIPTOR
        when (code) {
            INTERFACE_TRANSACTION -> {
                reply?.writeString(descriptor)
                return true
            }
            TRANSACTION_doSum -> {
                data.enforceInterface(descriptor)
                val _arg0 = data.readInt()
                val _arg1 = data.readInt()
                val _result = this.doSum(_arg0, _arg1)
                reply?.writeNoException()
                reply?.writeInt(_result)
                return true
            }
            TRANSACTION_doMinus -> {
                data.enforceInterface(descriptor)
                val _arg0 = data.readInt()
                val _arg1 = data.readInt()
                val _result = this.doMinus(_arg0, _arg1)
                reply?.writeNoException()
                reply?.writeInt(_result)
                return true
            }
            TRANSACTION_getPid -> {
                data.enforceInterface(descriptor)
                //val _arg0 = data.readInt()
                //val _arg1 = data.readInt()
                val _result = this.getPid()
                reply?.writeNoException()
                reply?.writeInt(_result)
                return true
            }
        }
        return super.onTransact(code, data, reply, flags)
    }

    companion object {
        const val DESCRIPTOR = "com.wolfsea.binderdemo.ICalculate"

        const val TRANSACTION_doSum = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0)
        const val TRANSACTION_doMinus = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1)
        const val TRANSACTION_getPid = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2)

        /**
         * Cast an IBinder object into an com.wolfsea.binderdemo.ICalculateNumber interface,
         * generating a proxy if needed.
         */
        fun asInterface(obj: android.os.IBinder?): ICalculateNumber? {
            if (obj == null) {
                return null
            }
            val iin = obj.queryLocalInterface(DESCRIPTOR)
            if (iin != null && iin is ICalculateNumber) {
                return iin
            }
            return CalculateNumberProxy(obj)
        }

        fun setDefaultImpl(impl: ICalculateNumber?): Boolean {
            if (CalculateNumberProxy.Companion.sDefaultImpl != null) {
                throw IllegalStateException("setDefaultImpl() called twice")
            }
            if (impl != null) {
                CalculateNumberProxy.Companion.sDefaultImpl = impl
                return true
            }
            return false
        }

        fun getDefaultImpl(): ICalculateNumber? = CalculateNumberProxy.Companion.sDefaultImpl
    }
}